const mongoose = require('mongoose');
const config = require('../config/config.js');
/**
 * [connect description] Connect to mongodb
 * @return {[type]} [description]
*/
var connect= function(){
    var options = {
        server:{
            socketOptions:{
                keepAlive:1 //保持长连接
            }
        }
    };
    mongoose.connect(config.mongooseConfig,options);
}
//start connect...
connect();

mongoose.connection.on('open',function(){
    console.log('mongodb exceptions: %s',err);
});
var count = 0;
//reconnet when closed
mongoose.connection.on('disconnected',function(){
    console.warn('mongodb disconnected... reconnecting %s second',count);
    count++;
    if(count <= 5){
        setTimeout(connect,3000);
    }else{
        process.exit(1);
    }
})

exports.mongoose = mongoose;